function addLayer(preset) {
  // 检查是否有打开的文档
  if (documents.length > 0) {
    var activeDocument = app.activeDocument;

    if (preset == 1) {
      //判断图层0
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "0") {
          ifExist = true;
        }
      }
      if (ifExist) {
        //建立图形图层
        var ifExist = false;
        for (i = 0; i < activeDocument.layers.length; i++) {
          if (activeDocument.layers[i].name == "图形") {
            ifExist = true;
          }
        }
        if (!ifExist) {
          newLayer = activeDocument.layers.add();
          newLayer.name = "图形";
          newLayer.zOrder(ZOrderMethod.SENDTOBACK);
        }
      } else {
        //更改默认图层名称
        var ifExist = false;
        for (i = 0; i < activeDocument.layers.length; i++) {
          if (activeDocument.layers[i].name == "图形") {
            ifExist = true;
          }
        }
        if (!ifExist) {
          currentLayer = activeDocument.layers[0];
          currentLayer.name = "图形";
        }
      }
      //建立备份图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "备份") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "备份";
        newLayer.zOrder(ZOrderMethod.SENDTOBACK);
      }
      //建立背景图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "背景") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "背景";
        newLayer.zOrder(ZOrderMethod.SENDTOBACK);
      }
      //创建背景
      // 获取画板两点坐标
      var artboardX1 = activeDocument.artboards[0].artboardRect[0];
      var artboardY1 = activeDocument.artboards[0].artboardRect[1];
      var artboardX2 = activeDocument.artboards[0].artboardRect[2];
      var artboardY2 = activeDocument.artboards[0].artboardRect[3];
      var artboardWidth = artboardX2 - artboardX1;
      var artboardHeight = artboardY1 - artboardY2;
      //定位背景图层
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "背景") {
          currentLayer = activeDocument.layers[i];
        }
      }
      //创建矩形
      var sizeScale = 1.35;
      var lengthAddon = (sizeScale - 1) * artboardWidth;
      var XOffset = lengthAddon / 2;
      var YOffset = XOffset;
      var cube = currentLayer.pathItems.rectangle(artboardX1 + XOffset, artboardY1 - YOffset, artboardWidth + lengthAddon, artboardHeight + lengthAddon);
      //改变颜色
      fillColor = new CMYKColor();
      fillColor.cyan = 0;
      fillColor.magenta = 0;
      fillColor.yellow = 0;
      fillColor.black = 15;
      cube.fillColor = fillColor;
      //锁住背景图层
      currentLayer.locked = true;

      //建立tag图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "标注") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "标注";
        newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
      }
      //建立cut图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "切割") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "切割";
        newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
      }
    } else if (preset == 2) {
      //更改默认图层名称
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "图形") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        currentLayer = activeDocument.layers[0];
        currentLayer.name = "图形";
      }
      //创建基本图形
      // 获取画板两点坐标
      var artboardX1 = activeDocument.artboards[0].artboardRect[0];
      var artboardY1 = activeDocument.artboards[0].artboardRect[1];
      var artboardX2 = activeDocument.artboards[0].artboardRect[2];
      var artboardY2 = activeDocument.artboards[0].artboardRect[3];
      var artboardWidth = artboardX2 - artboardX1;
      var artboardHeight = artboardY1 - artboardY2;
      //定位背景图层
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "图形") {
          currentLayer = activeDocument.layers[i];
        }
      }
      //创建矩形
      var cube = currentLayer.pathItems.rectangle(artboardX1, artboardY1, artboardWidth, artboardHeight);
      //改变样式
      activeDocument.graphicStyles[1].applyTo(cube);

      //建立bk图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "备份") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "备份";
        newLayer.zOrder(ZOrderMethod.SENDTOBACK);
      }
      //建立PSD图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "PSD") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "PSD";
        newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
      }
      //建立tag图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "标注") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "标注";
        newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
      }
      //建立cut图层
      var ifExist = false;
      for (i = 0; i < activeDocument.layers.length; i++) {
        if (activeDocument.layers[i].name == "切割") {
          ifExist = true;
        }
      }
      if (!ifExist) {
        newLayer = activeDocument.layers.add();
        newLayer.name = "切割";
        newLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
      }
    }
  } else {
    alert("请先打开一个文档!");
  }
}
